package cmd_gen

import (
	"gitee.com/xiakai001/goutl/errutl"
	"gitee.com/xiakai001/goutl/goutl"
	"github.com/urfave/cli/v2"
	"path"
	"path/filepath"
	"strings"
)

var DDLCli = &cli.Command{

	Name:        "ddl",
	Usage:       "把ddl转化为结构体",
	Description: "把ddl转化为结构体",

	Action: ddlAction,
	Flags: []cli.Flag{
		ddlPathFlag,
		outputFlag,
	},
}

var ddlPathFlag = &cli.StringFlag{
	Name:     "path",
	Aliases:  []string{"p"},
	Usage:    "ddl文件路径",
	Required: true,
}

var outputFlag = &cli.StringFlag{
	Name:     "output",
	Aliases:  []string{"o"},
	Usage:    "输出文件夹",
	Required: true,
}

func ddlAction(ctx *cli.Context) error {

	ddlPath := ctx.String("path")
	if len(ddlPath) == 0 {
		return errutl.New("ddlPath is empty")
	}
	output := ctx.String("output")
	if len(output) == 0 {
		return errutl.New("output is empty")
	}

	outFileName := strings.TrimSuffix(filepath.Base(ddlPath), filepath.Ext(filepath.Base(ddlPath)))
	if len(outFileName) == 0 {
		return errutl.New("outFileName is empty")
	}

	packageName := path.Base(output)
	err := goutl.GenerateGoFileFromDDL(packageName, ddlPath, path.Join(output, outFileName+"_model.go"))

	return err
}
